
package com.insigma.business.qrcode.soldier._bff.impl;


import cn.hutool.core.bean.BeanUtil;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common._consumer.CommonRepresentationRecordConsumer;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.RepresentationTypeEnum;
import com.insigma.business.qrcode.soldier.dto.CommonRepresentationRecordDTO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.CommonRepresentationRecordInsertRequest;
import com.insigma.business.qrcode.soldier.request.CommonRepresentationRecordSearchRequest;
import com.insigma.business.qrcode.soldier._bff.CommonRepresentationRecordBFFProvider;
import com.insigma.business.qrcode.soldier.util.BizCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonRepresentationRecordBFFProvider")
public class CommonRepresentationRecordBFFProviderImpl extends BaseProviderImpl implements CommonRepresentationRecordBFFProvider {

    @Autowired
    private CommonRepresentationRecordConsumer commonRepresentationRecordConsumer;


    @Override
    public BasePagingResponse<List<CommonRepresentationRecordDTO>> searchPlacementRepresentationRecord(CommonRepresentationRecordSearchRequest searchRequest) {

        searchRequest.setSortKeyValueJoint("createDatetime_desc");
        searchRequest.setType(RepresentationTypeEnum.PLACEMENT.name());
        BasePagingResponse<List<CommonRepresentationRecordDTO>> listBasePagingResponse = commonRepresentationRecordConsumer.searchCommonRepresentationRecord(searchRequest);

        return listBasePagingResponse;
    }


    @Override
    public BaseResponse<CommonRepresentationRecordDTO> getCommonRepresentationRecord(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonRepresentationRecordDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonRepresentationRecord的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<CommonRepresentationRecordDTO> commonRepresentationRecordResponse = commonRepresentationRecordConsumer.getCommonRepresentationRecord(baseGetRequest);
                BeanUtil.copyProperties(commonRepresentationRecordResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_REPRESENTATION_RECORD_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonRepresentationRecord(final CommonRepresentationRecordInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonRepresentationRecord的请求参数不能为空");
            }
            @Override
            public void executeService() {
                BaseResponse<String> commonRepresentationRecordResponse = commonRepresentationRecordConsumer.insertCommonRepresentationRecord(insertRequest);
                BeanUtil.copyProperties(commonRepresentationRecordResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_REPRESENTATION_RECORD_INSERT;
            }
        });
        return baseResponse;
    }

}
